package com.zrl.ggkt.wechat.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrl.ggkt.exception.MyException;
import com.zrl.ggkt.model.wechat.Menu;
import com.zrl.ggkt.vo.wechat.MenuVo;
import com.zrl.ggkt.wechat.mapper.MenuMapper;
import com.zrl.ggkt.wechat.service.MenuService;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单明细 订单明细 服务实现类
 * </p>
 *
 * @author zrl
 * @since 2022-08-17
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    private WxMpService wxMpService;

    //获取全部菜单
    @Override
    public List<MenuVo> findMenuInfo() {
        //1创建List集合﹐用户最终数据封装
        List<MenuVo> res = new ArrayList<>();
        //2查询所有菜单数据（包含一级和二级)
        List<Menu> menus = baseMapper.selectList(null);

        //3从所有菜单数据获取所有一级菜单数据( parent_id=o )
        List<Menu> oneMenuList = menus.stream()
                .filter(menu -> menu.getParentId().longValue() == 0)
                .collect(Collectors.toList());

        //4封装一级菜单数据﹐封装到最终数据list集合
        oneMenuList.stream().forEach(menu -> {
            MenuVo menuVo = new MenuVo();
            BeanUtils.copyProperties(menu,menuVo);

            //5封装二级菜单数据（判断一级菜单id和二级菜单parent_ id是含相同)
            //如果相同把二级菜单数据放到一级菜单里面
            List<Menu> twoMenuList = menus.stream()
                    .filter(menu1 -> menu1.getParentId() == menu.getId())
                    .collect(Collectors.toList());

            //List<Menu>--->List<MenuVo>
            List<MenuVo> menuVoList = new ArrayList<>();
            twoMenuList.stream().forEach(menu1 -> {
                MenuVo menuVo1 = new MenuVo();
                BeanUtils.copyProperties(menu1,menuVo1);
                menuVoList.add(menuVo1);
            });

            menuVo.setChildren(menuVoList);
            res.add(menuVo);
        });
        return res;
    }

    //获取一级菜单
    @Override
    public List<Menu> findMenuOneInfo() {
        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Menu::getParentId,0);
        List<Menu> menus = baseMapper.selectList(queryWrapper);
        return menus;
    }

    //同步菜单
    @Override
    public void synMenu() {
        //获取所有菜单数据
        List<MenuVo> menuVoList = this.findMenuInfo();
        //封装button内部结构
        JSONArray buttonList = new JSONArray();
        for (MenuVo oneMenuVo : menuVoList) {
            //一级菜单
            JSONObject one = new JSONObject();
            one.put("name",oneMenuVo.getName());

            //二级菜单
            JSONArray subButton = new JSONArray();
            for (MenuVo twoMenuVo : oneMenuVo.getChildren()) {
                JSONObject view = new JSONObject();
                view.put("type",twoMenuVo.getType());

                if (twoMenuVo.getType().equals("view")){
                    view.put("name",twoMenuVo.getName());
                    view.put("url", "http://ggkt.liulifrpnode3.ml/#"
                            +twoMenuVo.getUrl());
                }else{
                    view.put("name",twoMenuVo.getName());
                    view.put("key", twoMenuVo.getMeunKey());
                }
                subButton.add(view);
            }

            one.put("sub_button",subButton);
            buttonList.add(one);
        }
        //封装最外层button
        JSONObject button = new JSONObject();
        button.put("button",buttonList);
        try {
            String menuId =
                    this.wxMpService.getMenuService().menuCreate(button.toJSONString());
            System.out.println("menuId:"+ menuId);
        } catch (WxErrorException e) {
            e.printStackTrace();
            throw new MyException(20001,"公众号同步失败");
        }
    }

    //删除公众号菜单
    @Override
    public void removeMenu() {
        try {
            wxMpService.getMenuService().menuDelete();
        } catch (WxErrorException e) {
            e.printStackTrace();
            throw new MyException(20001,"公众号删除失败");
        }
    }
}
